package com.zpark.wxshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zpark.wxshop.Utils.redisUtil;
import com.zpark.wxshop.Vo.CartVo;
import com.zpark.wxshop.Vo.OrderInfoVo;
import com.zpark.wxshop.Vo.ShopVo;
import com.zpark.wxshop.entity.Cart;
import com.zpark.wxshop.entity.Shop;
import com.zpark.wxshop.mapper.CartMapper;
import com.zpark.wxshop.service.ICartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author PJone
 * @since 2023-09-04
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private redisUtil redisUtil;

    @Override
    public List<ShopVo> findByUid(Integer uid) {
        List<ShopVo> shopVoList = new ArrayList<>();
        List<CartVo> list = cartMapper.SelectByUid(uid);
        List<Integer> shopIds = cartMapper.SelectShopIdInCart(uid);
        for (Integer shopid : shopIds) {
            List<CartVo> cartVoList = new ArrayList<>();
            ShopVo shopVo = new ShopVo();
            for (CartVo cartVo : list) {
                if (Objects.equals(shopid, cartVo.getId())) {
                    shopVo.setId(cartVo.getId());
                    shopVo.setShopName(cartVo.getShopName());
                    cartVoList.add(cartVo);
                }
            }
            shopVo.setCartVoList(cartVoList);
            shopVoList.add(shopVo);
        }
        return shopVoList;


//        List<Map<String, List<CartVo>>> mapList = new ArrayList<>();
//        List<CartVo> list = cartMapper.SelectByUid(uid);
//        List<Integer> shopIds = cartMapper.SelectShopIdInCart(uid);
//        for (Integer shopid : shopIds){
//            Map<String, List<CartVo>> map = new HashMap<>();
//            String ShopName = "";
//            List<CartVo> ShopcartVoList = new ArrayList<>();
//            for (CartVo cartVo : list){
//                if (Objects.equals(shopid, cartVo.getId())){
//                    ShopName = cartVo.getShopName();
//                    ShopcartVoList.add(cartVo);
//                }
//            }
//            map.put(ShopName, ShopcartVoList);
//            mapList.add(map);
//        }
//        return mapList;
    }

    @Cacheable(cacheNames = "Card:checkProducts#600", key = "#OrderId")
    public OrderInfoVo findByCids(List<Integer> cids, String OrderId) {
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        List<ShopVo> shopVoList = new ArrayList<>();
        List<CartVo> list = cartMapper.SelectByCids(cids);
        List<Integer> shopIds = cartMapper.SelectShopIdInCheckCart(cids);
        Integer AllNum = 0;
        long AllPrice = 0;
        for (Integer shopid : shopIds) {
            List<CartVo> cartVoList = new ArrayList<>();
            ShopVo shopVo = new ShopVo();
            for (CartVo cartVo : list) {
                if (Objects.equals(shopid, cartVo.getId())) {
                    shopVo.setShopName(cartVo.getShopName());
                    shopVo.setId(cartVo.getId());
                    shopVo.setAllNum(AllNum += cartVo.getNum());
                    shopVo.setAllPrice((int) (AllPrice += (cartVo.getPrice() * cartVo.getNum())));
                    cartVoList.add(cartVo);
                }
            }
            shopVo.setCartVoList(cartVoList);
            shopVoList.add(shopVo);
        }
        orderInfoVo.setShopVoList(shopVoList);
        orderInfoVo.setOrderid(OrderId);
        return orderInfoVo;
    }
}
